import {
	dataReport
} from '@/api/currency.js'

export function dataReportFun(data) {

	let newData = {
		...data,
		happen_at: "" + parseInt(new Date().getTime() / 1000),
		ref_query: "",
		uid: ""
	}

	// 来源路劲
	if (uni.getStorageSync('ref_query')) {
		newData.ref_query = uni.getStorageSync('ref_query')
	}

	// 用户id
	if (uni.getStorageSync('userId')) {
		newData.uid = uni.getStorageSync('userId')
	}

	// 页面访问统计
	if (newData.event == "client.page_view") {
		newData.path = getCurrentPageInfo().currentPath;
		newData.fullPath = getCurrentPageInfo().fullPath;
	}

	// 调用接口
	dataReport(newData).then(res => {})
}


// 获取当前页面的路径及参数
function getCurrentPageInfo() {
	const pages = getCurrentPages(); // 获取页面栈
	const currentPage = pages[pages.length - 1]; // 获取当前页面
	const currentPath = currentPage.route; // 当前页面的路径
	const currentParams = currentPage.options; // 当前页面的参数
	const fullPath = currentPage?.$page?.fullPath;

	return {
		currentPath,
		currentParams,
		fullPath
	};
}